package com.foodlocker.maintenance;

import java.util.Timer;
import java.util.TimerTask;

import com.foodlocker.machine.MainActivity;
import com.foodlocker.machine.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.RadioGroup;

public class MaintenancerAct  extends FragmentActivity{

	Handler mBoxManageHandler;
	Timer  noTouchTimer;
	RadioGroup functionchoose;
	Fragment box_manage_fragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_maintenance);
		define_mBoxManageHandler();
		functionchoose= (RadioGroup) findViewById(R.id.function_choose_maintenance);
		
	   box_manage_fragment= new BoxManageFragment();
	   FragmentManager fragmentManager = getSupportFragmentManager();
       FragmentTransaction  transaction = fragmentManager.beginTransaction();	      
	   transaction.replace(R.id.container_maitenance,  box_manage_fragment).commit();  	    
	   define_All_Listener();  
	}

	  class NoTouchTask extends TimerTask{

			@Override
			public void run() {
				// TODO Auto-generated method stub			                     	             	
				 // 定义一个消息传过去  子线程无法改动UI线程里的空间，所以用消息去更改
	            Message msg = new Message(); 
	            msg.what = 5; 
	            mBoxManageHandler.sendMessage(msg); 
			}
	    	 
	     }
	/*	@Override
		public void onUserInteraction() {
			// TODO Auto-generated method stub
			super.onUserInteraction();
			noTouchTimer.cancel();
			noTouchTimer=new Timer();
			noTouchTimer.schedule( new NoTouchTask(), 10*1000);
		} */

private void define_mBoxManageHandler(){
		
		mBoxManageHandler= new Handler(){
			//   private static final String MAIN_TAG = "MainThread";
	        @Override
	        public void handleMessage(Message msg) {
	            Log.i("MainThread", "Got an incoming message from the child thread -what is: "+msg.what+"obj is:"
	                    + (String) msg.obj);

	            // 接收子线程的消息
	         //   info.setText((String) msg.obj + String.valueOf(nClick));
	        if(msg.what==2) { }
	            
	        // 长时间不操作，回到主界面。
	        if(msg.what==5){	       
	        	Intent intent= new Intent(MaintenancerAct.this, MainActivity.class);
				startActivity(intent);
				MaintenancerAct.this.finish();						        
	        }
	        }
	    };
	}
   private void define_All_Listener(){
	   
	   functionchoose.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(R.id.boxmanage_maintenance==checkedId) {
					 FragmentManager fragmentManager = getSupportFragmentManager();
				       FragmentTransaction  transaction = fragmentManager.beginTransaction();	      
					   transaction.replace(R.id.container_maitenance,  box_manage_fragment).commit();  	
				}
				if(R.id.machine_status_maintenance==checkedId) {
					
				}
			}
		});
   }	

}
